package testings;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Collections;

import Kmeans.mSimpleKmeans;
import LOF.LetUsBegin;

import weka.clusterers.ClusterEvaluation;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.PrincipalComponents;

public class fusion {
	//private static int minUB;
	private static int learnedK;
	public static void main(String args[]) throws Exception {
		
		//get the dataset

		BufferedReader reader = new BufferedReader(new FileReader("cloudy.arff"));

		Instances dataset=new Instances(reader);
		
		//doFusionGmeans(dataset);
		doFusionKmeansplusplus(dataset, 5);

		
		//Instances nDataset=testPCA(dataset);
//        GraphIt gf = new GraphIt();
//        gf.changeTitle("From instances, scatterplot");
//        gf.addToSeries(20,nDataset,0); //using x and y attributes
//        //gf.addToSeries(nDataset, 0, 1)
//        gf.initateGraphing();
//		
	}
	public static void doFusionKmeansplusplus(Instances dataset,int numClusters) throws Exception{
		
		int minUB=dokmeansplusplus(numClusters, dataset);
		Instances remove=new Instances(removeOutliers(dataset, minUB));
		minUB=dokmeansplusplus(numClusters, remove);
	}
	public static Instances removeOutliers(Instances dataset,int minUB){
		Instances data=new Instances(dataset);
		LetUsBegin x = new LetUsBegin(data, 15, minUB-1);
        int[] outliers = x.getOutliers();
        for(int i = outliers.length-1; i >0; i--){
            System.out.println("index of outlier: "+outliers[i]);
            data.remove(i);
        }
        
//        for(int i:outliers){
//        	data.remove(i);
//        }
        return data;
	}
	public static int dokmeansplusplus(int numClusters,Instances dataset) throws Exception{

		mSimpleKmeans sk=new mSimpleKmeans();
//		String[] options = new String[1];
//		options[0]="P";
//		sk.setOptions(options);
		sk.setNumClusters(numClusters);
		sk.setInitializeUsingKMeansPlusPlusMethod(true);
		sk.buildClusterer(dataset);
		
		ClusterEvaluation  eval =new ClusterEvaluation ();
		eval.setClusterer(sk);
	    eval.evaluateClusterer(dataset);
	    System.out.println(eval.clusterResultsToString());
	    return sk.getMinUB(dataset);
		
	}
	public static void doKmeans(int numClusters,Instances dataset) throws Exception{
		
		mSimpleKmeans sk=new mSimpleKmeans();
		sk.setNumClusters(numClusters);
		sk.buildClusterer(dataset);
		//System.out.println("minUB= "+sk.getMinUB(dataset));
		//System.out.println("number of C="+sk.getNumClusters());
		ClusterEvaluation  eval =new ClusterEvaluation ();
		eval.setClusterer(sk);
	    eval.evaluateClusterer(dataset);
	    System.out.println(eval.clusterResultsToString());
	    
	}
	
	public static Instances  testPCA(Instances data){
		
		PrincipalComponents p = new PrincipalComponents();
		Instances newData = null;
		try {
			p.setInputFormat(data);
			p.setMaximumAttributes(2);
			newData=Filter.useFilter(data, p);
			//System.out.println(newData.get(0));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newData;
		
	}
	public static void doFusionGmeans(Instances dataset) throws Exception{
		
		int minUB=doGmeans(dataset);
		doKmeans(learnedK, dataset);
		Instances removed=removeOutliers(dataset, minUB);
		doKmeans(learnedK, removed);
		
	}
//	public static Instances doFusiononGmeans(Instances dataset) throws Exception{
//		int temp=Integer.MIN_VALUE;
//		Instances data=new Instances(dataset);
//		do{
//			temp=learnedK;
//			int minUB=doGmeans(data);//learnedK are changed, if not, the loop is finished
//			System.out.println("minUB= "+minUB);
//			LetUsBegin x = new LetUsBegin(data, 10, minUB-1);
//		        int[] outliers = x.getOutliers();
//		        for(int i = 0; i < outliers.length; i ++){
//		            System.out.println("index of outlier: "+outliers[i]);
//		        }
//		     for(int i:outliers){
//		    	 data.remove(i);
//		     }
//		     System.out.println("temp= "+temp+"\n"+"learnedK= "+learnedK);
//		}while(temp!=learnedK);
//		
//		return data;
//	}
	
	public static int doGmeans(Instances dataset) throws Exception{
		int numCluster=0;
		Gmeans gm=new Gmeans(dataset);
		//Instances n=new Instances(gm.doPCA(dataset, 1));
		numCluster=gm.doGmeans();
		learnedK=gm.doGmeans();
		int minUB=gm.getMinUB();
		System.out.println("learned number of K ="+numCluster);
		return minUB;
	}
}
